﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        /**variables **/
        // Grid
        public static PlayGrid THEgrid = new PlayGrid();
        //interface for piece
        public tetriminos itest;
        //timer
        private System.Timers.Timer testTimer;
        public int score=0;
        public int multiplayer_enable;
        // multiplayer
        public tcpstuff mymanager;
        public String event_response = "test";
        public String data_to_send = "";
        public String data_receive = "player2 score is 0";
        String name = "";
        public String[] words;
        public static bool PartyGoing = false;
        /**End variables **/


        //random piece function
        public void MyRandom()
        {
            Console.WriteLine(event_response);
            Random random = new Random();
            int randomNumber = random.Next(0, 4);

            if (randomNumber == 0)
            {
                itest = new I();
                if (THEgrid.endgame() == true)
                {
                    THEgrid = new PlayGrid();
                    PartyGoing = false;
                }
            }
            else if (randomNumber == 1)
            {
                itest = new Z();
                if (THEgrid.endgame() == true)
                {
                    THEgrid = new PlayGrid();
                    PartyGoing = false;
                }
            }
            else if (randomNumber == 2)
            {
                itest = new S();
                if (THEgrid.endgame() == true)
                {
                    THEgrid = new PlayGrid();
                    PartyGoing = false;
                }
            }
            else if (randomNumber == 3)
            {
                itest = new O();
                if (THEgrid.endgame() == true)
                {
                    THEgrid = new PlayGrid();
                    PartyGoing = false;
                }
            }
        }

        //default constructor
        public Form1(){
            InitializeComponent();
        }

        //constructor ovorloader
        //pour avoir un timer
        public Form1(System.Timers.Timer testTimer)
        {
            InitializeComponent();
            this.testTimer = testTimer;
            multiplayer_enable = 0;
        }

        /** Delegate **/
        /* thread safe calling*/
        //refresh screen
        private delegate void refreshmyscreendelegate();

        private void refreshmyscreen()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(
                    new refreshmyscreendelegate(refreshmyscreen));
            }
            else
            {
                this.Refresh();

                label8.Text = DateTime.Now.ToString("HH:mm:ss");
            }
        }

        public delegate void multiscore();

        public void refreshmultiscore()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(
                    new multiscore(refreshmultiscore));
            }
            else
            {
                this.Refresh();
            }
        }


        public delegate void setscoredelegate();

        public void setscore()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(
                    new setscoredelegate(setscore));
            }
            else
            {
                label6.Text = words[0];
                label7.Text = words[3];
            }
        }

        //update score
        private delegate void refreshmyscoredelegate();

        private void refreshmyscore()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(
                    new refreshmyscoredelegate(refreshmyscore));
            }
            else
            {
                label2.Text = score.ToString();
            }
        }
        /** End Delegate **/

        //fonction nouvelle partie
        private void newgame()
        {
            bool containmyname;
            if (itest.MyBigMoveFunction("b", THEgrid, 0, 1) == 0)
            {
                score = THEgrid.ValidateGrid(score);
                refreshmyscore();
                itest = null;
                MyRandom();
                data_to_send = name + " score is " + score.ToString();
                if (multiplayer_enable == 1)
                {
                    mymanager.Send(data_to_send);
                    data_receive = mymanager.get_data_receive();
                    containmyname = data_receive.Contains(name);
                    if (!containmyname)
                    {
                        words = data_receive.Split(' ');
                        setscore();
                    }
                }
            }
        }

        //fonction liée au timer
        //devrait faire la meme chose que fleche bas
        private void OnTimerElapsed(object source, ElapsedEventArgs e)
        {
            newgame();
            refreshmyscreen();

        }

        //si on touche le clavier
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            String myvariable;

            if (itest != null)
            {
                switch (e.KeyData)
                {
                    case Keys.Up:
                        itest.fil(THEgrid, false);
                        itest.rotate();
                        itest.fil(THEgrid, true);
                        this.Refresh();
                        break;

                    case Keys.Left:
                        myvariable = "+";
                        if (itest.MyBigMoveFunction(myvariable, THEgrid, -1, 0) == 0)
                        {
                        }
                        this.Refresh();
                        break;

                    case Keys.Right:
                        myvariable = "-";
                        if (itest.MyBigMoveFunction(myvariable, THEgrid, 1, 0) == 0)
                        {
                        }
                        this.Refresh();
                        break;

                    case Keys.Down:
                        myvariable = "b";

                        newgame();

                        this.Refresh();
                        break;
                }
            }
        }

        /** elements graphiques*/
        // main window
        private void panel1_Paint_1(object sender, PaintEventArgs e)
        {
            using (Graphics g = this.panel1.CreateGraphics())
            {
                THEgrid.DrawGrid(g);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(Form1_KeyDown);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        //new game button
        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            THEgrid = new PlayGrid();
            PartyGoing = true;
            testTimer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
            testTimer.Enabled = true;
            MyRandom();
            score = 0;
            if (multiplayer_enable == 1)
            {
                mymanager.Recieve();
            }
        }

        //multiplayer button
        private void multiplayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (multiplayer_enable == 0)
            {
                mymanager = new tcpstuff();
                multiplayer_enable = 1;
                label4.Text = "enable";
            }
            else
            {
                multiplayer_enable = 0;
                label4.Text = "disable";
            }
        }



        //quit buttun
        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            name = textBox1.Text;
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }
        /** Fin elements graphiques*/

    }
}
